<?php

class Resultats_model extends CI_Model {
	
	protected $table;

	function __construct(){
		parent::__construct();
		$this->table = 'resultats';
	}

	
	
	function insert($num, $part, $pos){
		$this->db->insert($this->table, array('num_course'=>$num, 'id_part'=>$part, 'position'=>$pos));
	}

	function get_resultats_by_course($id){
		$res = 	$this->db->get_where($this->table, array('num_course'=>$id));
		if($res->num_rows() != 0)
			return $res->result_array();
		else
			return array();
	}

	function remove_resultats($course){
		$this->db->delete($this->table, array('num_course'=>$course));
	}

	function get_points_finaux($id){
		$res = $this->db->query("	SELECT SUM(`position`) AS sum FROM (
							SELECT `position` FROM resultats WHERE `id_part`=".$id." ORDER BY(`position`) LIMIT 0,6) AS sel");
		if($res->num_rows() == 1){
			$res = $res->result_array();
			return $res[0]['sum'];
		}
			
		else
			return array();
	}
}
?>